// pages/music/music.js

import { myRequest } from '../../utils/request';
import api from '../../utils/api'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        posterList: [],
        hotSingerList: [],
        hotAlbumList: [],
        hotMusicMenuList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       this.getPosterList();
       this.getHotAlbumList();
       this.getHotSingerList();
       this.getHotMusicMenuList();
    },

    // 获取海报数据
    async getPosterList() {
        const res = await myRequest({
            url: '/app/poster/list',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
                e.photo = api.BASE_URL + '/photo/view?filename=' + e.photo;
            });
            this.setData({posterList: result});
        }
    },

    // 跳转更多热门歌手列表
    goMoreHotSinger() {
        wx.navigateTo({
            url: '/pages/singerList/singerList?hot=true'
        })
    },

    // 跳转更多热门歌单列表
    goMoreHotMusicMenu() {
        wx.navigateTo({
            url: '/pages/musicMenu/musicMenu?hot=true'
        })
    },

    // 跳转到音乐列表页面
    goToMusicList(event) {
        const data = event.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/musicList/musicList?type=${data.type}&searchId=${data.id}`
        })
    },

    // 跳转更多热门专辑列表
    goMoreHotAlbum() {
        wx.navigateTo({
            url: '/pages/albumList/albumList?hot=true'
        })
    },

    // 获取热门歌手数据
    async getHotSingerList() {
        const res = await myRequest({
            url: '/app/singer/hot',
            method: 'POST',
            data: {page: 1, size: 4}
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
                e.headPic = api.BASE_URL + '/photo/view?filename=' + e.headPic;
            });
            this.setData({hotSingerList: result});
        }
    },

      // 获取热门专辑数据
      async getHotAlbumList() {
        const res = await myRequest({
            url: '/app/album/hot',
            method: 'POST',
            data: {page: 1, size: 4}
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
                e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            this.setData({hotAlbumList: result});
        }
    },

      // 获取热门歌单数据
      async getHotMusicMenuList() {
        const res = await myRequest({
            url: '/app/music/menu/hot',
            method: 'POST',
            data: {page: 1, size: 4}
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
                e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            this.setData({hotMusicMenuList: result});
        }
    },

    // 跳转至排行榜页面
    goToRank() {
        wx.navigateTo({
            url: '/pages/rank/rank'
        })
    },


     // 跳转至歌单页面
     goToMusicMenuList() {
        wx.navigateTo({
            url: '/pages/musicMenu/musicMenu'
        })
    },

    // 跳转至专辑页面
    goToAlbumList() {
        wx.navigateTo({
            url: '/pages/albumList/albumList'
        })
    },

    // 跳转至搜索页面
    goToSearch() {
        wx.navigateTo({
            url: '/pages/search/search'
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getPosterList();
        this.getHotAlbumList();
        this.getHotSingerList();
        this.getHotMusicMenuList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})